package bamboo.drawingkid.views;

import java.util.Vector;

import javax.microedition.lcdui.Graphics;

import bamboo.drawingkid.views.components.Button;
import bamboo.drawingkid.views.components.ToolbarButton;

/**
 * This class creates view for all controls as in spec
 * @author Cong Ho
 * 
 */
public abstract class ToolBarView {
	public static int HEIGHT = 45;
	public static int ICON_SPACE = 7;
	public static int ICON_LEFT_INDENT = 20;

    private int x = 0;
    private int y = 0;
    private int width;
    private int height;

    protected Vector tools;

    //buttons
    protected ToolbarButton btnQuit;
    protected ToolbarButton btnNew;
    protected ToolbarButton btnOpen;
    protected ToolbarButton btnSave;
    protected ToolbarButton btnColor;
    protected ToolbarButton btnBrushSize;
    protected ToolbarButton btnEraser;
    protected ToolbarButton btnTemplate;
    protected ToolbarButton btnSend;
    protected ToolbarButton btnInfo;
    
    private int buf_x = 0;
    private int buf_y = 0;

    private int last_x = 0;
    private int last_y = 0;

    public ToolBarView(int screenwidth, int screenheight) {
        width = screenwidth;
        HEIGHT = 45;
        tools = new Vector(10);
        addBasicTools();
    }

    private void addBasicTools() {
    	btnColor = new ToolbarButton("/Icon_Color_Hover.png", "/Icon_Color.png");
    	btnBrushSize = new ToolbarButton("/Icon_Brush_Hover.png", "/Icon_Brush.png");
    	btnEraser = new ToolbarButton("/Icon_Eraser_Hover.png", "/Icon_Eraser.png");
    	btnTemplate = new ToolbarButton("/Icon_Template_Hover.png", "/Icon_Template.png");
    	btnSend = new ToolbarButton("/Icon_Email_Hover.png", "/Icon_Email.png");
		btnOpen= new ToolbarButton("/Icon_Open_Hover.png", "/Icon_Open.png");
    	btnNew = new ToolbarButton("/Icon_New_Hover.png", "/Icon_New.png");
    	btnSave = new ToolbarButton("/Icon_Save_Hover.png", "/Icon_Save.png");
    	btnInfo = new ToolbarButton("/Icon_Info_Hover.png", "/Icon_Info.png");
    	btnQuit = new ToolbarButton("/Icon_Quit_Hover.png", "/Icon_Quit.png");
    	
    	addTool(btnColor);
    	addTool(btnBrushSize);
    	addTool(btnEraser);
    	addTool(btnTemplate);
    	addTool(btnSend);
    	addTool(btnOpen);
    	addTool(btnNew);
    	addTool(btnSave);
    	addTool(btnInfo);
    	addTool(btnQuit);
    	setToolsPositions();
    }

    protected void setToolsPositions() {
        int l = tools.size();
        
        //starting positions
        int xpos = ICON_LEFT_INDENT	;
        int ypos = 5;
        for(int i = 0; i < 2; i++) {
        	for(int j = 0; j < 5; j++) {
	            Button tool = (Button) tools.elementAt(5*i + j);
	            tool.setPosition(xpos, ypos);
	            xpos += tool.getWidth() + ICON_SPACE;
        	}
        	xpos = ICON_LEFT_INDENT;
        	ypos = 320 - HEIGHT + 5;
        }
    }

    /**
     * Render this toolbar
     * @param g graphic tool to draw this toolbar
     */
    public void render(Graphics g) {
        int l = tools.size();
        for(int i = 0; i < l; i++) {
            Button tool = (Button)tools.elementAt(i);
            tool.render(g);
        }
    }

    public void addTool(ToolbarButton tool, int index) {
        tools.insertElementAt(tool, index);
        setToolsPositions();
    }

    private void addTool(ToolbarButton tool) {
        tools.insertElementAt(tool, tools.size());
    }

    public void pointerPressed(int x, int y) {
        int l = tools.size();
        for(int i = 0; i < l; i++) {
            Button tool = (Button)tools.elementAt(i);
            tool.pressed(x-buf_x, y);
        }
       System.out.println("pressed button");
    }
    
    public void pointerUnpressed(int x, int y) {
        int l = tools.size();
        for(int i = 0; i < l; i++) {
            Button tool = (Button)tools.elementAt(i);
            tool.unpressed(x-buf_x, y);
        }
    }

    public void dragToolBar(int x, int y) {
        if (last_x == 0) {
            last_x = x;
        }
        int diff = x - last_x;
        if (diff > 0) {
            buf_x += 4;
            last_x = 0;
        } else if (diff < 0) {
            buf_x -= 4;
            last_x = 0;
        }

    }
	public void setLast_y(int last_y) {
		this.last_y = last_y;
	}
	public int getLast_y() {
		return last_y;
	}
	public void setY(int y) {
		this.y = y;
	}
	public int getY() {
		return y;
	}
	public void setX(int x) {
		this.x = x;
	}
	public int getX() {
		return x;
	}
    
}
